package com.attilax.util;

import java.util.Map;
import java.util.Set;
import java.util.function.Function;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.attilax.io.filex;
import com.attilax.json.AtiJson;
import com.attilax.json.JsonUtil;
import com.google.common.collect.Maps;
@SuppressWarnings("all")
public class JsonFieldMapper {
	
 
	public static void main(String[] args) {
		String f="C:\\Users\\Administrator\\Desktop\\中联文档\\single.txt.json";
		String s=filex.read(f,"gbk");
		Map  jo1 = JSON.parseObject(s);
	//	Map m=JsonFieldMapper.map(jo1,"displayName:name,duty.dutyName:department");
	//	System.out.println( m);
		System.out.println("");
	}

	public static Map map(JSONObject jo1, String filedMapTable_String,Function<Map, Map> fun) {
		// TODO Auto-generated method stub
		Map tabCol_kvMap = new JsonFieldMapper().getTabCol_kvMap(jo1,filedMapTable_String);
		return  fun.apply( tabCol_kvMap);
	}
	private Map get_filedMap(String filedMapTable) {
		filedMapTable = filedMapTable.trim();
		String[] items = filedMapTable.split(",");
		Map m = Maps.newConcurrentMap();
		for (String item : items) {
			String[] kv_a = item.split(":");
			m.put(kv_a[0].trim(), kv_a[1]);
		}

		return m;
	}
	private Map getTabCol_kvMap(JSONObject jo1, String filedMapTable_jsonKeyVsCol) {
		Map	filedMap = get_filedMap(filedMapTable_jsonKeyVsCol);
//{displayName=name, dudy.dutyName=department}
	
		Set<String> st = filedMap.keySet();

		Map Tab_filed_kv_Map = Maps.newConcurrentMap();
		int n=0;
		for (String jsonKey : st) {
			n++;
			if(n==2)
				System.out.println("dbg");
			Object jsonV =JsonUtil.get_canRetNull (   jo1,jsonKey);
			String tabCol = (String) filedMap.get(jsonKey);
			if(tabCol==null)
				throw new RuntimeException(" tabcol cant be null, jsonkey:"+jsonKey +"  ,filedMapTable_jsonKeyVsCol:"+filedMapTable_jsonKeyVsCol) ;
			if(jsonV!=null )
				Tab_filed_kv_Map.put(tabCol, jsonV);
		}

		JsonUtil.copyProp(jo1,Tab_filed_kv_Map);
		return Tab_filed_kv_Map;
	}

}
